export default {
    data() {
        return {
            promotions: ['红包券', '免息券'],
            promotionIndex: 0,
            reds: [],
            coupons: [],
            redCount: 9999,
            redNo: 0,
            redSize: 10,
            redRefreshing: false,
            couponCount: 9999,
            couponNo: 0,
            couponSize: 10,
            couponRefreshing: false,
            securitiesImg: '/static/img/securities@3x.png',
            alreadyImg: '/static/img/already@3x.png',
            expiredImg: '/static/img/expired@3x.png',
            invalidImg: '/static/img/invalid@3x.png'
        }
    },
    computed: {
        noMoreRed() {
            return this.reds == null || this.reds.length >= this.redCount
        },
        noMoreCoupon() {
            return this.coupons == null || this.coupons.length >= this.couponCount
        }
    },
    mounted() {
        this.loadMoreCoupon()
        this.loadMoreRed()
    },
    methods: {
        // 加载红包
        loadMoreRed() {
            if (!this.noMoreRed || this.redNo == 0) {
                this.redNo++
                $ajax.user.redList({ userId: $store.state.user.userId, pageNo: this.redNo, pageSize: this.redSize }).then(res => {
                    var { code, msg, data } = res.data
                    if (code == 'S') {
                        if (this.redNo > 1) {
                            this.reds.push(...data.list)
                        } else {
                            this.reds = data.list
                        }
                        this.redCount = data.count
                    } else {
                        $toast.show(msg)
                        this.redNo--
                    }
                    this.closeRedLoad()
                }).catch(err => {
                    this.redNo--
                    this.closeRedLoad()
                })
            }
        },
        // 加载免息券
        loadMoreCoupon() {
            if (!this.noMoreCoupon || this.couponNo == 0) {
                this.couponNo++
                $ajax.user.freeList({ userId: $store.state.user.userId, pageNo: this.couponNo, pageSize: this.couponSize }).then(res => {
                    var { code, msg, data } = res.data
                    if (code == 'S') {
                        if (this.couponNo > 1) {
                            this.coupons.push(...data.list)
                        } else {
                            this.coupons = data.list
                        }
                        this.couponCount = data.count
                    } else {
                        $toast.show(msg)
                        this.couponNo--
                    }
                    this.closeCouponLoad()
                }).catch(err => {
                    this.couponNo--
                    this.closeCouponLoad()
                })
            }
        },
        // 下拉刷新红包
        reLoadRed() {
            this.redNo = 0
            this.redCount = 99
            this.redRefreshing = true
            this.loadMoreRed()
        },
        // 下拉刷新免息券
        reLoadCoupon() {
            this.couponNo = 0
            this.couponCount = 99
            this.couponRefreshing = true
            this.loadMoreCoupon()
        },
        // 关闭红包框下拉刷新和上拉加载
        closeRedLoad() {
            this.redRefreshing = false
            this.$refs.redScroller.infiniteDone()
            this.$refs.redScroller.refreshDone()
        },
        // 关闭免息券下拉刷新和上拉加载
        closeCouponLoad() {
            this.couponRefreshing = false
            this.$refs.couponScroller.infiniteDone()
            this.$refs.couponScroller.refreshDone()
        }
    },
    mixins: [$mixins.needLogin]
}